<?php

namespace App\Console\Commands\Cruise;

use App\Models\Cruise\CruiseOrderInputModel;
use App\Models\Cruise\CruiseOrderModel;
use App\Models\Cruise\CruiseOrderProjectLazyModel;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Mission\CheckInputService;
use App\Support\CustomForm\AttributeCruiseSupport;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class SyncCruiseOrderProjectLazyCommand extends Command
{
    protected $signature   = 'cruise:sync_cruise_order_project_lazy';
    protected $description = '通过cruise_order_project_lazy表数据';

    // 每几个小时执行一次
    const EXECUTE_HOUR = 6;

    public function handle()
    {
        ini_set('memory_limit', '1024M');

        // 每6小时一次吧
        $now = date('Y-m-d H:00:00');

        $start = strtotime($now) - self::EXECUTE_HOUR * 3600;
        $end   = strtotime($now) - 1;

        CruiseOrderInputModel::query()
                             ->join('cruise_order', 'cruise_order.id', '=', 'cruise_order_inputs.orderId')
                             ->where('cruise_order.type', CruiseTemplateModel::TYPE_3)
                             ->where('cruise_order.isDeleted', 0)
                             ->where('cruise_order.status', CruiseOrderModel::STATUS_COMPLETE)
                             ->whereBetween('cruise_order_inputs.updatedAt', [$start, $end])
                             ->select('cruise_order_inputs.*')
                             ->get()
                             ->each(function (CruiseOrderInputModel $cruiseOrderInputModel) {
                                 try {
                                     $this->syncCruiseOrderProjectLazy($cruiseOrderInputModel);
                                 } catch (\Exception $e) {
                                    $this->error($e);
                                 }

                                 $this->info($cruiseOrderInputModel->orderId);
                             });
    }

    private function syncCruiseOrderProjectLazy(CruiseOrderInputModel $cruiseOrderInputModel)
    {
        $inputs = (new CheckInputService($cruiseOrderInputModel->orderId, 0))
            ->getInputObjectFill()['lists'];

        $insertData = [];

        foreach ($inputs as $input) {
            $positionName = $input['positionName'];
            foreach ($input['projects'] as $project) {

                // 签名不处理
                if ($project['inputType'] == AttributeCruiseSupport::INPUT_TYPE_SIGNATURE) {
                    continue;
                }

                if (in_array($project['inputType'], AttributeCruiseSupport::CRUISE_CHECK_MODULE)) {
                    $content = AttributeCruiseSupport::CRUISE_CHECK_TEXT[$project['value']] ?? '';
                } else {
                    $content = '';
                }

                $insertData[] = [
                    'cruiseOrderId'  => $cruiseOrderInputModel->orderId,
                    'positionName'   => $positionName,
                    'field'          => $project['field'],
                    'name'           => $project['text'],
                    'content'        => $content,
                    'problemDesc'    => $project['customAttributes']['auditValue']['desc'] ?? '',
                    'rectifySuggest' => $project['customAttributes']['auditValue']['reason'] ?? '',
                    'createdAt'      => time(),
                ];
            }
        }

        DB::beginTransaction();

        CruiseOrderProjectLazyModel::query()->where('cruiseOrderId', $cruiseOrderInputModel->orderId)->delete();
        CruiseOrderProjectLazyModel::query()->insert($insertData);

        DB::commit();
    }
}
